package org.lmind.example.uc.webapp;

import java.lang.reflect.Method;

import org.lmind.example.uc.webapp.view.PageView;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;

public class ExampleModelAndViewResolver implements ModelAndViewResolver {

	@SuppressWarnings("rawtypes")
	@Override
	public ModelAndView resolveModelAndView(Method handlerMethod,
			Class handlerType, Object returnValue,
			ExtendedModelMap implicitModel, NativeWebRequest webRequest) {
		
		if (!(returnValue instanceof PageView)) {
			return UNRESOLVED;
		}
		PageView view = (PageView)returnValue;
		
		ModelAndView mv = new ModelAndView(view.getName());
		mv.addObject("model", view.getModel());
		
		return mv;
	}

}
